package com.itsu.job.components;

import com.itsu.job.config.Job;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.Resource;

/**
 * @author Jerry.Su
 * @Date 2021/12/31 14:32
 */
public abstract class LockScheduleHandler implements ScheduleHandler {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void handle(Job job) {
        if (RedisLockUtil.trylock(stringRedisTemplate, getLockKey(job), job.getLockTime().toMillis())) {
            doHandle(job);
        }
    }

    protected abstract void doHandle(Job job);

    protected String getLockKey(Job job) {
        return job.getJobId();
    }
}
